//
//  CAForumCommentCell.m
//  cascade-realm
//
//  Created by QSC on 3/6/24.
//

#import "CAForumCommentCell.h"
@interface CAForumCommentCell()
@property (nonatomic,strong)UIImageView * headImageV;
@property (nonatomic,strong)UILabel * nameLabel;
@property (nonatomic,strong)UILabel * contentLabel;
@property (nonatomic,strong)UIButton * likeBtn;
@property (nonatomic,strong)UIButton * voiceBtn;
@property (nonatomic,strong)UIView * boxView;
@end
@implementation CAForumCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUp];
    }
    
    return  self;
}

- (void)setComment:(CAComment *)comment{
    _comment = comment;
    [self.headImageV sd_setImageWithURL:[NSURL URLWithString:comment.avatar] placeholderImage:Img(@"defult_head")];
    self.nameLabel.text = comment.name;
    
    self.likeBtn.selected = [comment.likeUserIds containsObject:CAUserInfo.shared.userInfo.userId];
    
    if (comment.type == 1) {
        self.contentLabel.text = comment.content;
        self.contentLabel.hidden = NO;
        self.voiceBtn.hidden =  YES;
        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.boxView).offset(-10);
        }];
    }else{
        [self.voiceBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)comment.recordTime] forState:UIControlStateNormal];
        self.contentLabel.hidden = YES;
        self.voiceBtn.hidden =  NO;
        
        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.boxView).offset(-45);
        }];
        if (comment.playing) {
            [self.voiceBtn.imageView startAnimating];
        }else{
            [self.voiceBtn.imageView stopAnimating];
        }
    }
   
}


- (void)setUp{
    UIView * contentView = self.contentView;
    
    UIView * content = [UIView new];
    content.backgroundColor = Hex(0xFCFFF6);
    content.layer.cornerRadius = 8;
    [content setDefultBorder];
    self.boxView = content;
    [contentView addSubview:content];
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(contentView);
        make.left.equalTo(contentView).offset(20);
        make.top.equalTo(contentView).offset(15);
    }];
    
    UIImageView * headImgv = [UIImageView new];
    self.headImageV = headImgv;
    headImgv.layer.cornerRadius = 14;
    headImgv.clipsToBounds = YES;
    [content addSubview:headImgv];
    [headImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(content).offset(8);
        make.size.mas_equalTo(28);
    }];
    
    UILabel * nameLabel = [UILabel LabelWithFont:BoldFont(14) color:Hex(0x404040) text:@""];
    self.nameLabel = nameLabel;
    [content addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headImgv);
        make.left.equalTo(headImgv.mas_right).offset(8);
        make.right.equalTo(content).offset(-40);
    }];
    
    UIButton * voiceBtn = [UIButton ButtonWithFont:Font(16) color:[UIColor whiteColor] text:@"6S"];
    self.voiceBtn = voiceBtn;
    [voiceBtn setImage:Img(@"voice") forState:UIControlStateNormal];
    voiceBtn.backgroundColor = Hex(0xBAE701);
    voiceBtn.layer.cornerRadius = 14;
    [content addSubview:voiceBtn];
    voiceBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15);
    [voiceBtn sizeToFit];
    [voiceBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:8];
    [voiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(16);
        make.height.mas_equalTo(28);
    }];
    
    voiceBtn.imageView.animationImages = @[Img(@"voiceplay0"),Img(@"voiceplay1"),Img(@"voiceplay2")];
    voiceBtn.imageView.animationDuration = 1.0;
    [voiceBtn addTarget:self action:@selector(voiceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton * likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.likeBtn = likeBtn;
    [likeBtn setBackgroundImage:Img(@"detail_unlike") forState:UIControlStateNormal];
    [likeBtn setBackgroundImage:Img(@"detail_like") forState:UIControlStateSelected];
    [content addSubview:likeBtn];
    [likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headImgv);
        make.right.equalTo(content).offset(-16);
        make.size.mas_equalTo(20);
    }];
    [likeBtn addTarget:self action:@selector(likeClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel * contentLabel = [UILabel LabelWithFont:Font(10) color:[UIColor colorWithWhite:0 alpha:0.6] text:@""];
    [content addSubview:contentLabel];
    
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(content).inset(8);
        make.top.equalTo(headImgv.mas_bottom).offset(10);
        make.bottom.equalTo(content).offset(-10);
    }];
    contentLabel.numberOfLines = 0;
    self.contentLabel = contentLabel;
    
}


- (void)likeClick{
    
    self.likeBtn.selected = !self.likeBtn.selected;
    
    [Tool ShowLoading];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Tool HideLoading];
        if (self.comment.likeUserIds == nil) {
            self.comment.likeUserIds = [NSMutableArray array];
        }
        
        [self.comment.likeUserIds addObject:CAUserInfo.shared.userInfo.userId];
        !self.saveBlock?:self.saveBlock();
    });
    
    
}

- (void)voiceBtnClick{
    
    WeakSelf
    [RecordTool.shared playUrl:[NSURL fileURLWithPath:self.comment.path] finishBlock:^{
        
        [weakSelf.voiceBtn.imageView stopAnimating];
        weakSelf.comment.playing = NO;
    }];
    [self.voiceBtn.imageView startAnimating];
    self.comment.playing = YES;
    
}

@end
